package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.constant.DictionaryConstants;
import com.zjuee.base.dto.common.ledger.LedgerProduceDto;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.enums.RedisCacheType;
import com.zjuee.base.exception.ServiceException;
import com.zjuee.base.model.common.company.Company;
import com.zjuee.base.model.common.plan.PlanDanger;
import com.zjuee.base.model.common.plan.PlanSolid;
import com.zjuee.base.model.common.plan.PlanTreat;
import com.zjuee.base.utils.DateUtils;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.base.model.common.ledger.LedgerProduce;
import com.zjuee.base.model.common.ledger.LedgerProduceBill;
import com.zjuee.base.model.common.ledger.LedgerProduceGather;
import com.zjuee.service.*;
import com.zjuee.service.company.service.CompanyService;
import com.zjuee.service.company.vo.CompanyInfoVo;
import com.zjuee.service.company.vo.CompanyVo;
import com.zjuee.web.ledger.mapper.LedgerProduceBillMapper;
import com.zjuee.web.ledger.mapper.LedgerProduceGatherMapper;
import com.zjuee.web.ledger.mapper.LedgerProduceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@Service
public class LedgerProduceServiceImpl implements LedgerProduceService {


    @Resource
    private LedgerProduceBillMapper ledgerProduceBillMapper;
    @Resource
    private LedgerProduceGatherMapper ledgerProduceGatherMapper;

    @Resource
    private LedgerProduceMapper ledgerProduceMapper;
    @Resource
    private CompanyService companyService;
    @Resource
    LedgerProduceService ledgerProduceService;

    @Resource
    PlanDangerService planDangerService;
    @Resource
    PlanSolidService planSolidService;
    @Resource
    PlanTreatService planTreatService;
    @Resource
    PlanService planService;
    @Resource
    TranslateData translateData;

    @Resource
    private LedgerDangerSubtotalService ledgerDangerSubtotalService;

    @Resource
    private LedgerSolidSubtotalService ledgerSolidSubtotalService;
    @Resource
    private LoginDto loginDto;

    @Override
    @Transactional
    public Long add(LedgerProduce ledgerProduce) {
        Company company = companyService.selectById(loginDto.getOrgBusinessId(),null).getCompany();
        ledgerProduce.setWlpModel(ledgerProduce.getWlpType().substring(0, 1));
        LedgerProduceGather ledgerProduceGather = null;

        //不包含医废，医废没有计划，所以没有初始化Gather表，也不用校验计划产生量
        if (!DictionaryConstants.TYPE_MEDICAL.equals(ledgerProduce.getWlpType())) {
            //查询产生台账年度汇总表
            LedgerProduceGather ledgerProduceGatherCon = new LedgerProduceGather();
            ledgerProduceGatherCon.setBciId(company.getId());
            ledgerProduceGatherCon.setWlpgType(ledgerProduce.getWlpType());
            ledgerProduceGatherCon.setWlpgYear(String.valueOf(DateUtils.getYear(new Date())));
            ledgerProduceGatherCon.setWpiId(ledgerProduce.getWpiId());
            ledgerProduceGatherCon.setWlpgScode(ledgerProduce.getWlpScode());

            if (DictionaryConstants.TYPE_DANGER.equals(ledgerProduce.getWlpType())) {
                ledgerProduceGatherCon.setWlpgName(ledgerProduce.getWlpName());
            }
            ledgerProduceGather = ledgerProduceGatherMapper.findOneByCondition(ledgerProduceGatherCon);

            //判断产生量是否大于计划产生量
            //实际产生总量   实际产生总量 = 产生量 + 当年线下产生量（非初次填报可能为null） + 汇总表的总产生量
            BigDecimal produceAmount = ledgerProduce.getWlpNumber()
                    .add(ledgerProduce.getWlpThisShift() == null ? BigDecimal.ZERO : ledgerProduce.getWlpThisShift())
                    .add(ledgerProduceGather == null ? BigDecimal.ZERO : ledgerProduceGather.getWlpgTotalProduce());

            ledgerProduce.setWlpBcode(ledgerProduceGather.getWlpgBcode());
            //危废
            if (DictionaryConstants.TYPE_DANGER.equals(ledgerProduce.getWlpType())) {
                PlanDanger planDangerCon = new PlanDanger();
                planDangerCon.setWpiId(ledgerProduce.getWpiId());
                planDangerCon.setWpdBcode(ledgerProduce.getWlpBcode());
                planDangerCon.setWpdScode(ledgerProduce.getWlpScode());
                planDangerCon.setWpdName(ledgerProduce.getWlpName());
                List<PlanDanger> planDangerList = planDangerService.getPlanDanger(planDangerCon);

                if (CollectionUtils.isEmpty(planDangerList)) {
                    throw new ServiceException("危废产生计划不存在!");
                }
                PlanDanger planDanger = planDangerService.getPlanDanger(planDangerCon).get(0);

                if (planDanger.getWpdThisProduce().compareTo(produceAmount) < 0) {
                    throw new ServiceException("产生量不能大于计划产生量!");
                }

            }

            //固废
            if (DictionaryConstants.TYPE_SOLID.equals(ledgerProduce.getWlpModel())) {
                PlanSolid planSolidCon = new PlanSolid();
                planSolidCon.setWpiId(ledgerProduce.getWpiId());
                planSolidCon.setWpsCode(ledgerProduce.getWlpScode());
                List<PlanSolid> planSolidList = planSolidService.getPlanSolid(planSolidCon);
                if (CollectionUtils.isEmpty(planSolidList)) {
                    throw new ServiceException("固废产生计划不存在!");
                }
                PlanSolid planSolid = planSolidList.get(0);

                if (planSolid.getWpsThisProduce().compareTo(produceAmount) < 0) {
                    throw new ServiceException("产生量不能大于计划产生量!");
                }
            }

            //查询自行利用处置计划  如果没有计划 自行处置设为0
            PlanTreat planTreatCon = new PlanTreat();
            planTreatCon.setWpiId(ledgerProduce.getWpiId());
            planTreatCon.setWptScode(ledgerProduce.getWlpScode());
            planTreatCon.setWptType(ledgerProduce.getWlpType());
            List<PlanTreat> planTreatList = planTreatService.getPlanTreat(planTreatCon);
            if (CollectionUtils.isEmpty(planTreatList)) {
                ledgerProduce.setWlpDispose(BigDecimal.ZERO);
            }
        } else {
            //先进行单位换算
            this.conversion(ledgerProduce);
            //医废查询gather数据
            LedgerProduceGather ledgerProduceGatherCon = new LedgerProduceGather();
            ledgerProduceGatherCon.setBciId(company.getId());
            ledgerProduceGatherCon.setWlpgType(String.valueOf(DictionaryConstants.TYPE_MEDICAL));
            ledgerProduceGatherCon.setWlpgYear(String.valueOf(DateUtils.getYear(new Date())));
            ledgerProduceGatherCon.setWlpgScode(ledgerProduce.getWlpScode());
            ledgerProduceGather = ledgerProduceGatherMapper.findOneByCondition(ledgerProduceGatherCon);
        }

        BigDecimal currStock = BigDecimal.ZERO;
        Date date = new Date();

        //如果是初次填报
        if (DictionaryConstants.WL_INIT_IS.equals(ledgerProduce.getWlpInit())) {
            if (ledgerProduceGather != null) {
                //计算当前库存  当前库存=年度汇总表库存+产生量+线下产生量-自行利用处置量-当年线下委托处置量
                currStock = ledgerProduceGather.getWlpgStock().add(ledgerProduce.getWlpNumber()).add(ledgerProduce.getWlpThisShift()).subtract(ledgerProduce.getWlpDispose()).subtract(ledgerProduce.getWlpThisDispose());
                ledgerProduceGather.setWlpgModifiedTime(date);
                ledgerProduceGather.setWlpgModifier(loginDto.getUserName());
            } else {
                //gather表为空时表示为医废初次填报
                //计算当前库存  gather表为null时 当前库存=填报表上年库存+产生量+线下产生量-自行利用处置量-当年线下委托处置量
                currStock = ledgerProduce.getWlpPreStock().add(ledgerProduce.getWlpNumber())
                        .add(ledgerProduce.getWlpThisShift()).subtract(ledgerProduce.getWlpDispose())
                        .subtract(ledgerProduce.getWlpThisDispose());
                ledgerProduceGather = new LedgerProduceGather();
                ledgerProduceGather.setWlpgYear(String.valueOf(DateUtils.getYear(new Date())));
                ledgerProduceGather.setWpiId(ledgerProduce.getWpiId());
                ledgerProduceGather.setWlpgModel(ledgerProduce.getWlpModel());
                ledgerProduceGather.setWlpgType(ledgerProduce.getWlpType());
                ledgerProduceGather.setBciId(company.getId());
                ledgerProduceGather.setWlpgBcode(ledgerProduce.getWlpBcode());
                ledgerProduceGather.setWlpgScode(ledgerProduce.getWlpScode());
                ledgerProduceGather.setWlpgName(ledgerProduce.getWlpName());
                ledgerProduceGather.setWlpgPreStock(ledgerProduce.getWlpPreStock());
                ledgerProduceGather.setWlpgTotalTrustDispose(BigDecimal.ZERO);
                ledgerProduceGather.setWlpgChange(BigDecimal.ZERO);
                ledgerProduceGather.setWlpgCanDelete(DictionaryConstants.COMMON_CHOOSE_DEFAULT);
                ledgerProduceGather.setWlpgCanEdit(DictionaryConstants.COMMON_CHOOSE_DEFAULT);
                ledgerProduceGather.setWlpgCreateTime(date);
                ledgerProduceGather.setWlpgCreator(loginDto.getUserName());
            }

            //与前端传过来的库存进行校验
            if (currStock.compareTo(ledgerProduce.getWlpStock()) != 0) {
                throw new ServiceException("保存失败! 库存不一致");
            }

            //更新或插入ledgerProduceGather
            ledgerProduceGather.setWlpgTotalProduce(ledgerProduce.getWlpNumber().add(ledgerProduce.getWlpThisShift()));
            ledgerProduceGather.setWlpgUnderProduce(ledgerProduce.getWlpThisShift());
            ledgerProduceGather.setWlpgUnderDispose(ledgerProduce.getWlpThisDispose());
            ledgerProduceGather.setWlpgTotalDispose(ledgerProduce.getWlpDispose());
            ledgerProduceGather.setWlpgTotalTrustDispose(ledgerProduceGather.getWlpgTotalTrustDispose().add(ledgerProduce.getWlpThisDispose()));
            ledgerProduceGather.setWlpgStock(currStock);
            ledgerProduceGatherMapper.updateById(ledgerProduceGather);

        } else {

            if (DictionaryConstants.TYPE_MEDICAL.equals(ledgerProduce.getWlpType()) &&
                    ledgerProduceGather == null) {
                //医废查询上年gather数据
                LedgerProduceGather ledgerProduceGatherCon = new LedgerProduceGather();
                ledgerProduceGatherCon.setBciId(company.getId());
                ledgerProduceGatherCon.setWlpgType(DictionaryConstants.TYPE_MEDICAL);
                ledgerProduceGatherCon.setWlpgYear(String.valueOf(Integer.valueOf(DateUtils.getYear(new Date())) - 1));
                ledgerProduceGatherCon.setWlpgScode(ledgerProduce.getWlpScode());
                LedgerProduceGather ledgerProduceGatherPre = ledgerProduceGatherMapper.findOneByCondition(ledgerProduceGatherCon);

                if (ledgerProduceGatherPre == null) {
                    throw new ServiceException("保存失败! 上年台账不存在");
                }

                ledgerProduceGather = new LedgerProduceGather();
                ledgerProduceGather.setWlpgYear(String.valueOf(DateUtils.getYear(new Date())));
                ledgerProduceGather.setWpiId(0L);
                ledgerProduceGather.setWlpgModel(ledgerProduceGatherPre.getWlpgModel());
                ledgerProduceGather.setWlpgType(ledgerProduce.getWlpType());
                ledgerProduceGather.setBciId(company.getId());
                ledgerProduceGather.setWlpgBcode(ledgerProduce.getWlpBcode());
                ledgerProduceGather.setWlpgScode(ledgerProduce.getWlpScode());
                ledgerProduceGather.setWlpgName(ledgerProduce.getWlpName());
                ledgerProduceGather.setWlpgPreStock(ledgerProduceGatherPre.getWlpgStock());
                ledgerProduceGather.setWlpgUnderProduce(BigDecimal.ZERO);
                ledgerProduceGather.setWlpgUnderDispose(BigDecimal.ZERO);
                ledgerProduceGather.setWlpgTotalTrustDispose(BigDecimal.ZERO);
                ledgerProduceGather.setWlpgChange(BigDecimal.ZERO);
                ledgerProduceGather.setWlpgCanDelete(DictionaryConstants.COMMON_CHOOSE_DEFAULT);
                ledgerProduceGather.setWlpgCanEdit(DictionaryConstants.COMMON_CHOOSE_DEFAULT);
                ledgerProduceGather.setWlpgTotalProduce(ledgerProduce.getWlpNumber());
                ledgerProduceGather.setWlpgTotalDispose(ledgerProduce.getWlpDispose());
                ledgerProduceGather.setWlpgStock(ledgerProduceGatherPre.getWlpgStock().add(ledgerProduce.getWlpNumber()).subtract(ledgerProduce.getWlpDispose()));
                ledgerProduceGather.setWlpgCreateTime(date);
                ledgerProduceGather.setWlpgCreator(loginDto.getUserName());
                ledgerProduceGatherMapper.insert(ledgerProduceGather);
            } else {
                //非初次填报
                //企业可能将当年台账删除->重新填报 需要查询上年台账数据生成新的当年的台账
                if (ledgerProduceGather == null) {
                    //查询上年gather数据
                    LedgerProduceGather ledgerProduceGatherCon = new LedgerProduceGather();
                    ledgerProduceGatherCon.setBciId(company.getId());
                    ledgerProduceGatherCon.setWlpgType(ledgerProduce.getWlpType());
                    ledgerProduceGatherCon.setWlpgYear(String.valueOf(Integer.valueOf(DateUtils.getYear(new Date())) - 1));
                    ledgerProduceGatherCon.setWlpgScode(ledgerProduce.getWlpScode());
                    ledgerProduceGatherCon.setWlpgName(ledgerProduce.getWlpName());
                    LedgerProduceGather ledgerProduceGatherPre = ledgerProduceGatherMapper.findOneByCondition(ledgerProduceGatherCon);

                    if (ledgerProduceGatherPre == null) {
                        throw new ServiceException("保存失败! 上年台账不存在");
                    }

                    ledgerProduceGather = new LedgerProduceGather();
                    ledgerProduceGather.setWlpgYear(String.valueOf(DateUtils.getYear(new Date())));
                    ledgerProduceGather.setWpiId(ledgerProduce.getWpiId());
                    ledgerProduceGather.setWlpgModel(ledgerProduceGatherPre.getWlpgModel());
                    ledgerProduceGather.setWlpgType(ledgerProduce.getWlpType());
                    ledgerProduceGather.setBciId(company.getId());
                    ledgerProduceGather.setWlpgBcode(ledgerProduce.getWlpBcode());
                    ledgerProduceGather.setWlpgScode(ledgerProduce.getWlpScode());
                    ledgerProduceGather.setWlpgName(ledgerProduce.getWlpName());
                    ledgerProduceGather.setWlpgPreStock(ledgerProduceGatherPre.getWlpgStock());
                    ledgerProduceGather.setWlpgUnderProduce(BigDecimal.ZERO);
                    ledgerProduceGather.setWlpgUnderDispose(BigDecimal.ZERO);
                    ledgerProduceGather.setWlpgTotalTrustDispose(BigDecimal.ZERO);
                    ledgerProduceGather.setWlpgChange(BigDecimal.ZERO);
                    ledgerProduceGather.setWlpgCanDelete(DictionaryConstants.COMMON_CHOOSE_DEFAULT);
                    ledgerProduceGather.setWlpgCanEdit(DictionaryConstants.COMMON_CHOOSE_DEFAULT);
                    ledgerProduceGather.setWlpgTotalProduce(ledgerProduce.getWlpNumber());
                    ledgerProduceGather.setWlpgTotalDispose(ledgerProduce.getWlpDispose());
                    ledgerProduceGather.setWlpgStock(ledgerProduceGatherPre.getWlpgStock().add(ledgerProduce.getWlpNumber()).subtract(ledgerProduce.getWlpDispose()));
                    ledgerProduceGather.setWlpgCreateTime(date);
                    ledgerProduceGather.setWlpgCreator(loginDto.getUserName());
                    ledgerProduceGatherMapper.insert(ledgerProduceGather);

                } else {
                    //计算当前库存 当前库存=年度汇总表库存+产生量-自行利用处置量
                    currStock = ledgerProduceGather.getWlpgStock().add(ledgerProduce.getWlpNumber()).subtract(ledgerProduce.getWlpDispose());

                    //与前端传过来的库存进行校验
                    if (currStock.compareTo(ledgerProduce.getWlpStock()) != 0) {
                        throw new ServiceException("保存失败! 库存不一致");
                    }

                    //更新年度汇总表
                    ledgerProduceGather.setWlpgTotalProduce(ledgerProduceGather.getWlpgTotalProduce().add(ledgerProduce.getWlpNumber()));
                    ledgerProduceGather.setWlpgTotalDispose(ledgerProduceGather.getWlpgTotalDispose().add(ledgerProduce.getWlpDispose()));
                    ledgerProduceGather.setWlpgStock(currStock);
                    ledgerProduceGather.setWlpgModifier(loginDto.getUserName());
                    ledgerProduceGather.setWlpgModifiedTime(date);
                    ledgerProduceGather.setWlpgCanDelete(DictionaryConstants.COMMON_CHOOSE_NOT);
                    ledgerProduceGatherMapper.updateById(ledgerProduceGather);
                }
            }
        }

        //插入填报记录
        ledgerProduce.setBciId(company.getId());
        ledgerProduce.setWlpStock(currStock);
        ledgerProduce.setWlpCreator(loginDto.getUserName());
        ledgerProduce.setWlpCreateTime(date);
        ledgerProduceMapper.insert(ledgerProduce);

        //插入bill表记录
        LedgerProduceBill ledgerProduceBill = new LedgerProduceBill();
        ledgerProduceBill.setBciId(company.getId());
        ledgerProduceBill.setWpiId(ledgerProduce.getWpiId());
        ledgerProduceBill.setWlpgId(ledgerProduceGather.getId());
        ledgerProduceBill.setWlpbFrom(DictionaryConstants.WL_FROM_WRITE);
        ledgerProduceBill.setWlpbBcode(ledgerProduce.getWlpBcode());
        ledgerProduceBill.setWlpbScode(ledgerProduce.getWlpScode());
        ledgerProduceBill.setWlpbName(ledgerProduce.getWlpName());
        ledgerProduceBill.setWlpbModel(Integer.parseInt(ledgerProduce.getWlpModel()));
        ledgerProduceBill.setWlpbType(Integer.parseInt(ledgerProduce.getWlpType()));
        ledgerProduceBill.setWlpbStock(ledgerProduceGather.getWlpgStock());
        ledgerProduceBill.setWlpbProduce(ledgerProduce.getWlpNumber());
        ledgerProduceBill.setWlpbDispose(ledgerProduce.getWlpDispose());
        ledgerProduceBill.setWlpbTrustDispose(BigDecimal.ZERO);
        ledgerProduceBill.setWlpbRemark(ledgerProduce.getWlpRemark());
        ledgerProduceBill.setWlpbCreator(loginDto.getUserName());
        ledgerProduceBill.setWlpbCreateTime(date);
        ledgerProduceBillMapper.insert(ledgerProduceBill);

        // 入统计表
        if (DictionaryConstants.TYPE_DANGER.equals(ledgerProduce.getWlpType())
                || DictionaryConstants.TYPE_MEDICAL.equals(ledgerProduce.getWlpType())) {
            ledgerDangerSubtotalService.ledgerDangerProduceReport(ledgerProduce, DictionaryConstants.VL_LEDGER_NOMAL);
        } else {

            ledgerSolidSubtotalService.ledgerSolidProduceReport(ledgerProduce, DictionaryConstants.VL_LEDGER_NOMAL);
        }

        return 1l;
    }


    /**
     * 单位换算 千克-->吨
     *
     * @param ledgerProduce
     */
    private void conversion(LedgerProduce ledgerProduce) {
        BigDecimal number = new BigDecimal(1000);
        BigDecimal zero = BigDecimal.ZERO;
        ledgerProduce.setWlpDispose(ledgerProduce.getWlpDispose() != null ? ledgerProduce.getWlpDispose().divide(number) : zero);
        ledgerProduce.setWlpStock(ledgerProduce.getWlpStock() != null ? ledgerProduce.getWlpStock().divide(number) : zero);
        ledgerProduce.setWlpNumber(ledgerProduce.getWlpNumber() != null ? ledgerProduce.getWlpNumber().divide(number) : zero);
        ledgerProduce.setWlpPreSelfDispose(ledgerProduce.getWlpPreSelfDispose() != null ? ledgerProduce.getWlpPreSelfDispose().divide(number) : zero);
        ledgerProduce.setWlpPreShift(ledgerProduce.getWlpPreShift() != null ? ledgerProduce.getWlpPreShift().divide(number) : zero);
        ledgerProduce.setWlpPreTrustDispose(ledgerProduce.getWlpPreTrustDispose() != null ? ledgerProduce.getWlpPreTrustDispose().divide(number) : zero);
        ledgerProduce.setWlpThisDispose(ledgerProduce.getWlpThisDispose() != null ? ledgerProduce.getWlpThisDispose().divide(number) : zero);
        ledgerProduce.setWlpThisShift(ledgerProduce.getWlpThisShift() != null ? ledgerProduce.getWlpThisShift().divide(number) : zero);
        ledgerProduce.setWlpPreStock(ledgerProduce.getWlpPreStock() != null ? ledgerProduce.getWlpPreStock().divide(number) : zero);
    }

    @Override
    public Map<String, Object> isInitLedgerProduce(LedgerProduce ledgerProduce) {
        if (ledgerProduce == null) {
            throw new ServiceException("查询失败");
        }

        CompanyVo companyVo = companyService.selectById(loginDto.getOrgBusinessId(), null);
        CompanyInfoVo company = companyVo.getCompany();
        if (company == null) {
            throw new ServiceException("企业信息错误!");
        }
        ledgerProduce.setBciId(company.getId());

        LedgerProduceGather ledgerProduceGatherCon = new LedgerProduceGather();
        ledgerProduceGatherCon.setBciId(company.getId());
        ledgerProduceGatherCon.setWpiId(ledgerProduce.getWpiId());
        ledgerProduceGatherCon.setWlpgType(ledgerProduce.getWlpType());
        ledgerProduceGatherCon.setWlpgYear(DateUtils.getYear(new Date()) + "");
        ledgerProduceGatherCon.setWlpgScode(ledgerProduce.getWlpScode());

        if (DictionaryConstants.MODEL_DANGER.equals(ledgerProduce.getWlpType().substring(0, 1))) {
            if (!DictionaryConstants.TYPE_MEDICAL.equals(ledgerProduce.getWlpType())) {
                ledgerProduceGatherCon.setWlpgName(ledgerProduce.getWlpName());
            } else {
                ledgerProduce.setWlpName(null);
            }
        }
        Map<String, Object> result = new HashMap<String, Object>();

        //查询台帐信息
        Long wpiId = ledgerProduce.getWpiId();
        List<LedgerProduce> ledgerProduceList = ledgerProduceService.findByCondition(ledgerProduce);
        LedgerProduceGather ledgerProduceGather = ledgerProduceGatherMapper.findOneByCondition(ledgerProduceGatherCon);

        String isInit = DictionaryConstants.WL_INIT_IS;

        if (ledgerProduceList != null && ledgerProduceList.size() > 0) {
            isInit = DictionaryConstants.WL_INIT_NOT;
        }

        if (ledgerProduceGather == null) {
            if (DictionaryConstants.TYPE_DANGER.equals(ledgerProduce.getWlpType())) {
                PlanDanger planDangerCon = new PlanDanger();
                planDangerCon.setWpdModel(Integer.parseInt(DictionaryConstants.WP_MODEL_DEFAULT));
                planDangerCon.setWpiId(wpiId);
                planDangerCon.setWpdType(Integer.parseInt(ledgerProduce.getWlpType()));
                planDangerCon.setWpdScode(ledgerProduce.getWlpScode());
                planDangerCon.setWpdName(ledgerProduce.getWlpName());
                List<PlanDanger> planDangerList = planDangerService.getPlanDanger(planDangerCon);

                if (planDangerList == null || planDangerList.size() == 0) {
                    throw new ServiceException("危废产生计划不存在");
                }
                ledgerProduceGather.setWlpgPreStock(planDangerList.get(0).getWpdPreStock());
                ledgerProduceGather.setWlpgStock(planDangerList.get(0).getWpdPreStock());
                result.put("dangerPlan", planDangerList);
            } else if (DictionaryConstants.TYPE_BSOLID.equals(ledgerProduce.getWlpType())
                    || DictionaryConstants.TYPE_SOLID.equals(ledgerProduce.getWlpType())) {
                PlanSolid planSolidCon = new PlanSolid();
                planSolidCon.setWpsModel(Integer.parseInt(DictionaryConstants.WP_MODEL_DEFAULT));
                planSolidCon.setWpsType(Integer.parseInt(ledgerProduce.getWlpType()));
                planSolidCon.setWpiId(wpiId);
                planSolidCon.setWpsCode(ledgerProduce.getWlpScode());
                List<PlanSolid> planSolidList = planSolidService.getPlanSolid(planSolidCon);

                if (planSolidList == null || planSolidList.size() == 0) {
                    throw new ServiceException("固废产生计划不存在");
                }

                ledgerProduceGather.setWlpgPreStock(planSolidList.get(0).getWpsPreStock());
                ledgerProduceGather.setWlpgStock(planSolidList.get(0).getWpsPreStock());
                result.put("solidPlan", planSolidList);

                if (DictionaryConstants.WL_INIT_NOT.equals(isInit) && DictionaryConstants.TYPE_MEDICAL.equals(ledgerProduce.getWlpType())) {
                    ledgerProduceGatherCon.setWlpgYear(String.valueOf(Integer.valueOf(DateUtils.getYear(new Date())) - 1));
                    LedgerProduceGather ledgerProduceGatherPre = ledgerProduceGatherMapper.findOneByCondition(ledgerProduceGatherCon);
                    ledgerProduceGather.setWlpgStock(ledgerProduceGatherPre.getWlpgStock());
                }
            }
        }
        //查询自行利用处置计划
        PlanTreat planTreatCon = new PlanTreat();
        planTreatCon.setWpiId(ledgerProduce.getWpiId());
        planTreatCon.setWptScode(ledgerProduce.getWlpScode());
        planTreatCon.setWptType(ledgerProduce.getWlpType());
        if (DictionaryConstants.MODEL_DANGER.equals(ledgerProduce.getWlpModel())) {
            planTreatCon.setWptName(ledgerProduce.getWlpName());
        }
        List<PlanTreat> planTreatList = planTreatService.getPlanTreat(planTreatCon);
        translateData.translateData(planTreatList,
                "LedgerTreatPlanHandle",
                String.valueOf(RedisCacheType.DISPOSE_TYPE),
                String.valueOf(RedisCacheType.DISPOSE_STYPE)
        );
        result.put("planTreatList", planTreatList);
        // 判断是否是医疗废物产生单位
        String dictionaryFilter = "IN[";

        if (company.getBciType().contains(DictionaryConstants.BCI_TYPE_DANGER_PRODUCTION)) {
            dictionaryFilter += DictionaryConstants.TYPE_DANGER + ",";
        }

        if (!StringUtils.isEmpty(company.getBciTag()) && DictionaryConstants.BCI_TAG_MEDICAL.equals(company.getBciTag())) {
            dictionaryFilter += DictionaryConstants.TYPE_MEDICAL + ",";
        }

        if (company.getBciType().contains(DictionaryConstants.BCI_TYPE_SOLID_PRODUCTION)) {
            dictionaryFilter += DictionaryConstants.TYPE_SOLID + "," + DictionaryConstants.TYPE_BSOLID + ",";
        }

        dictionaryFilter = dictionaryFilter.substring(0, dictionaryFilter.length() - 1) + "]";
        result.put("dictionaryFilter", dictionaryFilter);
        result.put("isInit", isInit);
        result.put("ledgerProduceGather", ledgerProduceGather);
        return result;
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(LedgerProduce type) {
        return false;
    }

    @Override
    public LedgerProduce selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<LedgerProduce> list(LedgerProduceDto dto) {
        return null;
    }

    @Override
    public LambdaQueryWrapper<LedgerProduce> fuzzyQuery(LedgerProduceDto dto) {
        return null;
    }

    @Override
    public List<LedgerProduce> findByCondition(LedgerProduce ledgerProduce) {
        List<LedgerProduce> ledgerProduceList = ledgerProduceMapper.produceList(ledgerProduce);
        return ledgerProduceList;
    }
}
